package com.kmproject.haifu.mvp.presenter;

import android.app.Application;

import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;

import io.reactivex.Observable;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

import javax.inject.Inject;

import com.jess.arms.utils.DataHelper;
import com.kmproject.haifu.app.AppConstant;
import com.kmproject.haifu.app.utils.AppUtil;
import com.kmproject.haifu.app.utils.RxUtils;
import com.kmproject.haifu.mvp.contract.UserResetPwdContract;
import com.kmproject.haifu.mvp.model.entity.ResultBean;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 06/23/2019 15:27
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@ActivityScope
public class UserResetPwdPresenter extends BasePresenter<UserResetPwdContract.Model, UserResetPwdContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public UserResetPwdPresenter(UserResetPwdContract.Model model, UserResetPwdContract.View rootView) {
        super(model, rootView);
    }

    /**
     * 注册
     * @param password
     * @param mobile
     * @param identifyCode
     */
    public void userRegist(String password,  String mobile, String identifyCode) {
        mModel.userRegist(password,"android",mobile,identifyCode).compose(RxUtils.applySchedulers(mRootView))
        .subscribe(new ErrorHandleSubscriber<ResultBean>(mErrorHandler) {
            @Override
            public void onNext(ResultBean stringResultBean) {
                if (AppUtil.isSuccess(stringResultBean))
                {
                    //保存用户id
                    mRootView.showMessage(stringResultBean.getMessage());
                    mRootView.userRegistSuccess();
                }else {
                    if (stringResultBean.getCode()==AppConstant.ACCOUNT_EXIST)
                    {
                        mRootView.userRegistFail(stringResultBean.getMessage());
                    }else {
                        mRootView.showMessage(stringResultBean.getMessage());
                    }

                }
            }
        });
    }

    /**
     * 重置密码
     * @param password
     * @param mobile
     * @param identifyCode
     */
    public void resetPwd(String password, String mobile, String identifyCode)
    {
        mModel.resetPwd(password,mobile,identifyCode).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean resultBean) {
                        if (AppUtil.isSuccess(resultBean))
                       {

                           mRootView.resetPwdSuccess();
                       }else {
                            mRootView.showMessage(resultBean.getMessage());
                        }
                    }
                });
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }
}
